<?php
/**
 * Created by PhpStorm.
 * User: 小夏
 * Date: 2016/10/11 0011
 * Time: 13:37
 */
namespace Payment\Controller;
use Common\Controller\HomebaseController;
class IndexController extends HomebaseController
{
    protected $course;
    protected $activity;
    protected $ordergoods;
    protected $orderinfo;
    function _initialize() {
        parent::_initialize();
        $this->course = M("Course");
        $this->activity = M("Activity");
        $this->ordergoods = M("OrderGoods");
        $this->orderinfo = M("OrderInfo");
    }
    function index(){
        if(sp_is_user_login()){
            $order_sn = I("get.snid");
            $result = $this->orderinfo->where(array('order_sn'=>$order_sn))->find();
            $goods = $this->ordergoods->where(array('order_id'=>$result['order_id']))->find();
            $result['goods_name'] = $goods['goods_name'];
            //print_r($order_sn);exit;
            $this->assign($result);
        }else{
            $this->error('您还未登录！');
        }

        $this->display(":active");
    }
    //生成订单
    function ceorder(){

        if(sp_is_user_login()){
            $goods_type = I('post.goods_type');

            $id = I('post.id');
            $where = array('id'=>$id);
            if($goods_type == 1){
                $chart = $this->course;
            }
            if($goods_type ==2){
                $chart = $this->activity;
            }
            $result  = $chart->field('uid,smeta,post_content',true)->where($where)->find();

            $userinfo = $_SESSION['user'];
            $order_sn = sp_get_order_sn();//订单号
            $data['order_sn'] =$order_sn;
            $data['user_id'] =$userinfo['id'];//用户id
            $data['order_status'] =0;//订单状态
            $data['pay_status'] =0;//支付状态
            $data['pay_type'] =1;//支付方式：1微信支付2支付宝支付3银联支付
            $data['consignee'] =$userinfo['user_nicename'];//用户
            $data['mobile'] =$userinfo['mobile'];
            $data['goods_amount'] =$result['shop_price'];//商品总金额
            $data['order_amount'] =$result['shop_price'];//应付款金额
            $data['add_time'] =time();
            $data['goods_type'] =$goods_type;//商品分类：1课程类2活动类3操盘类4充值类
            $orresult = $this->orderinfo->data($data)->add();
            if($orresult){
                $goodta['order_id'] =$orresult;
                $goodta['goods_id'] =$result['id'];
                $goodta['goods_name'] =$result['name'];
                $goodta['market_price'] =$result['market_price'];
                $goodta['shop_price'] =$result['shop_price'];
                $goodta['goods_type'] =$goods_type;
                $gdresult = $this->ordergoods->data($goodta)->add();
                if($gdresult){
                    $this->ajaxReturn(array("status"=>1,'order_sn'=>$order_sn));
                }else{
                    $this->ajaxReturn(array("status"=>0,"info"=>"操作失败！"));
                }
            }else{
                $this->ajaxReturn(array("status"=>0,"info"=>"操作失败！"));
            }

        }else{
            $this->ajaxReturn(array("status"=>0,"info"=>"您还未登录！"));
        }

    }
    //查询订单是否支付
    function orinquire(){
        $order_sn = I('post.order_sn');
        $result = $this->orderinfo->where(array('order_sn'=>$order_sn,'order_status'=>1,'pay_status'=>1))->find();
        if($result){
            $this->ajaxReturn(array("status"=>1,"succeeds"=>1,'order_sn'=>$order_sn));
        }else{
            $this->ajaxReturn(array("status"=>0,'order_sn'=>$order_sn,"info"=>"支付失败！"));
        }
    }
    //二维码
    public function code(){
        header("content-type:image/png;");
        Vendor("phpqrcode.phpqrcode");
        $QRcode = new \QRcode();
//        $url = json_decode($this->native());
        $data = base64_decode(I('get.url','',''));
//        $data = U("huying/home/index",array("id"=>I("get.id")),true,true);
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 8;
        // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
        //$path = "images/";
        // 生成的文件名
        //$fileName = $path.$size.'.png';
        $QRcode->png($data,false,$level,$size,1,false,0xFFFFFFFF);
        exit;
    }


    /**支付环节
     * @throws \WxPayException
     */
    public function native(){

        vendor("Wxpay.NativePay");
        $moneys =  I('get.money');
        $ordersn =  I('get.ordersn');
        $user = $_SESSION['user'];
        $username = $user['user_nicename'];
        $attach ='username='.$username.'&ordersn='.$ordersn.'&price='.$moneys;
        $order_sn = \WxPayConfig::MCHID.date("YmdHis");//订单号
        $totalmoney = $this->orderinfo->where(array('order_sn'=>$ordersn,'user_id'=>$user['id']))->find();


            if( $moneys == $totalmoney['order_amount']){
                //统一下单
                $notify = new \NativePay();
                $input = new \WxPayUnifiedOrder();
                $input->SetBody("课程教育");//商品描述
                $input->SetAttach($attach);//附加数据
                $input->SetOut_trade_no($order_sn);//商户订单号
                $input->SetTotal_fee($moneys*100);//金额
                $input->SetTime_start(date("YmdHis"));//交易起始时间
                $input->SetTime_expire(date("YmdHis", time() + 600));//交易结束时间
                $input->SetGoods_tag("粤高学堂");//商品标记
                $input->SetNotify_url("http://egao.yyw66.cn/index.php/Payment/demo/pay_return");//通知地址
                $input->SetTrade_type("NATIVE");//交易类型
                $input->SetProduct_id("123456789");//商品ID
                $result = $notify->GetPayUrl($input);//生成直接支付url，支付url有效期为2小时

                $url = $result["code_url"];
                //$this->redirect(U('portal/home/code',array('url'=>base64_encode($url))));
                $qrcode_url=U('Payment/Index/code',array('url'=>base64_encode($url)));

                $this->ajaxReturn(array('qrcode_url'=>$qrcode_url,'order_sn'=>$ordersn,'out_trade_no'=>$order_sn,'status'=>1));
            }else{
                $this->error('订单信息有误');
            }

    }
}